package com.stlm2.core.yun;

import org.apache.commons.lang.StringUtils;
import org.json.JSONArray;
import org.json.JSONObject;

/**
 * 子账号

 *
 */
public class SubAccount {
	
	public static void create(){
		String URL = Core.BASE_ACCOUNT_URL + "/SubAccounts";
		
		JSONObject json = new JSONObject();
		json.put("appId", Core.APP_ID);
		json.put("friendlyName", "common user");
		String result = Core.post(false, URL, json.toString());
		if(StringUtils.isBlank(result))
			return;
		
		json = new JSONObject(result);
		if(Core.SUCC_CODE.equals(json.getString("statusCode"))){
			JSONObject subAccount = json.getJSONObject("SubAccount");
			System.out.println("subAccountSid:" + subAccount.getString("subAccountSid"));
			System.out.println("subToken:" + subAccount.getString("subToken"));
		}
	}
	
	public static void get(){
		String URL = Core.BASE_ACCOUNT_URL + "/GetSubAccounts";
		
		JSONObject json = new JSONObject();
		json.put("appId", Core.APP_ID);
		String result = Core.post(false, URL, json.toString());
		if(StringUtils.isBlank(result))
			return;
		
		json = new JSONObject(result);
		if(Core.SUCC_CODE.equals(json.getString("statusCode"))){
			JSONArray subAccounts = json.getJSONArray("SubAccount");
			for(int i=0;i<subAccounts.length();i++){
				JSONObject obj = subAccounts.getJSONObject(i);
				System.out.println("++++++++++++++++++++++++++++++++++++");
				System.out.println("subAccountSid:" + obj.getString("subAccountSid"));
				System.out.println("subToken:" + obj.getString("subToken"));
			}
		}
	}
	
	public static void main(String [] args){
//		get();
		create();
	}
}
